package io.gitee.wiqer.common.core.utils;

import java.util.concurrent.ThreadLocalRandom;

public class NetIdUtils {
    public static Long getRandomIncrementId(){
        Long time=System.currentTimeMillis();
        int random= ThreadLocalRandom.current().nextInt()&0x7fffffff;
        return (random>>=10)|( time<<=22);
    }

    public static void main(String[] args) {
//        Long time=System.currentTimeMillis();
//        for (int i = 0; i < 10; i++) {
//            System.out.println(getRandomIncrementId() );
//            System.out.println(getRandomIncrementStringId() );
//            //getRandomIncrementId();
//        }
//        System.out.println("10w个id用时："+(System.currentTimeMillis()-time)+"ms");
        System.out.println(BigIntegerStringTo64String("1633957207779897227089372147712"));
    }

    public static String getRandomIncrementStringId(){
        Long time=System.currentTimeMillis();
        int random= ThreadLocalRandom.current().nextInt()&0x7fffffff;
        return EasyFastFormat.digits32ZeroBreak((random >>= 10)|( time<<=22),13);
    }
    public static String BigIntegerStringTo64String(String nub) {
        String id="";
        for (String nm:stringSpilt(nub,18) ) {
            //System.out.println("longToDate："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(nm))));
            if(!StringUtils.isEmpty(nm))
                id+=EasyFastFormat.digits64ZeroBreak(Long.parseLong(nm),10);
        }
        return id;
    }

    //将字符串按照一定长度分割
    public static String[] stringSpilt(String s,int len){
        String[] subs;//创建可分割数量的数组
        if(s.length()<=len){
            subs =new String[1];
            subs[0]=s;
            return subs;
        }
        //len->想要分割获得的子字符串的长度
        int spiltNum;
        int i;
        //存放需要切割的数组长度
        int cache = len;
        spiltNum = (s.length())/len;

        if((s.length()%len)>0){
            subs = new String[spiltNum+1];
        }else{
            subs = new String[spiltNum];
        }
        //可用一个全局变量保存分割的数组的长度
        int start = 0;
        if(spiltNum>0){
            for(i=0;i<subs.length;i++){
                if(i==0){
                    subs[0] = s.substring(start, len);
                    start = len;
                }else if(i>0 && i<subs.length-1){
                    len = len + cache ;
                    subs[i] = s.substring(start,len);
                    start = len ;
                }else{
                    subs[i] = s.substring(len);
                }
            }
        }
        return subs;
    }

}
